package com.aopcinemas.cinema.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.aopcinemas.cinema.model.Client;
import com.aopcinemas.cinema.model.Client.Status;

public class ClientDao {

    private Connection connection;

    public ClientDao(Connection connection) {
        this.connection = connection;
    }

    public List<Client> getClients() {
        List<Client> clients = new ArrayList<Client>();

        try {
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("select * from client");

            while (rs.next()) {
                Client client = new Client();
                populateCLient(rs, client);
                clients.add(client);
            }

            rs.close();
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }
        return clients;
    }

    public Client getClient(String uid) {
        Client client = null;

        try {
            PreparedStatement st = connection.prepareStatement("select * from client where uid = ?");
            st.setString(1, uid);

            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                client = new Client();
                populateCLient(rs, client);
            }
            rs.close();
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }

        return client;
    }

    private void populateCLient(ResultSet rs, Client client) throws SQLException {
        client.setId(rs.getLong("id"));
        client.setName(rs.getString("name"));
        client.setUid(rs.getString("uid"));
        client.setDateOfBirth(rs.getDate("date_of_birth"));
        String status = rs.getString("status");
        if (status != null) {
            client.setStatus(Status.valueOf(status));
        }
        client.setEmail(rs.getString("email"));
        client.setLanguage(rs.getString("language"));
    }

    public void addClient(Client client) {
        try {
            CallableStatement ct = connection.prepareCall("call new_client(?,?,?,?,?,?,?)");
            ct.registerOutParameter(1, Types.BIGINT);
            ct.setString(2, client.getName());
            ct.setDate(3, new Date(client.getDateOfBirth().getTime()));
            ct.setString(4, client.getUid());
            ct.setString(5, client.getLanguage());
            ct.setString(6, client.getEmail());
            ct.setString(7, client.getStatus() == null ? null : client.getStatus().toString());

            ct.execute();
            client.setId(ct.getLong(1));

            ct.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }
    }
}
